package localSearch;

import instrumentation.Condition;
import instrumentation.ConditionClass;
import instrumentation.ConditionIf;
import instrumentation.ConditionSwitch;
import instrumentation.Data;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import random.TestClusterBuilder;
import GA.GAInfo;
import ReadWGZ.ReadFilewgz;

public abstract class LocalSearchPopulation implements LocalSearch {
	protected GAInfo     gaInfo;

	public final void setExtraInfo(GAInfo gaInfo) {
		this.gaInfo     = gaInfo;

	}
	public final Map<Integer, Condition> readFileWGZ(TestClusterBuilder TestCluster)throws  IOException, ClassNotFoundException{
		 Set<Class<?>> classesToTest=TestCluster.getClassToTest();
		Map<Integer, Condition> mapBranchCondition= new HashMap<Integer, Condition>();
		ReadFilewgz readFile = new ReadFilewgz();
		for (Object obj : classesToTest) {
			Class<?> clazz=(Class<?>) obj;
			String classFileName=clazz.getName();
			
			classFileName=classFileName.replace('.', '/');
			classFileName=classFileName+".wgz";
			 classFileName="file:c:/MAMDA/MAMDA/instrumented/"+classFileName.toString();
			 Map<String, ConditionClass>  clazzes= readFile.read(classFileName);
			mapBranchCondition.putAll( readFile.buildInternalState(clazzes));
		}
		return  mapBranchCondition;
	}
	public void displayConditionAndRecommendMethods(Map<Integer, Map<Class<?>, List<String>>>   methodsRecommendPValues,Condition c,int trgtBranch) {
		System.out.println("Condition Info:");
//		Condition c= sorted_map.get(entry.getKey());
		System.out.print("\t"+c.getCName() + "." +c.getMName() + " ");		
		if(c instanceof ConditionIf )				
			System.out.print("[branch:"+((ConditionIf) c).getBranchId() + "] ");
		else{
			  System.out.print("[branch:"+trgtBranch + "] ");
			}
		System.out.print(c.getCallee1()+ " ");

		System.out.print(c.getV1()==null ?"null":c.getV1().toString()+ " ");
		if(c instanceof ConditionIf){
			System.out.print(c.getV2()==null?"null":c.getV2().toString());
			System.out.print(" PredicateTpe: "+c.getType().toString());
		}
		else
			System.out.print("\tcases: " + ((ConditionSwitch) c).getCases().get(trgtBranch));
		
		System.out.print( c.getIsCallMethod()?" call Method":"" );
		
		System.out.println("");
		List<String> methodsList = new LinkedList<String>();
		Set <Integer> keyData= methodsRecommendPValues.keySet();

		for (Integer v : keyData) {
			Map<Class<?>, List<String>> methodsRecommend= methodsRecommendPValues.get(v);
			System.out.println("methods Recommend List for : v" + v.toString());
			for(Class<?> clazz:methodsRecommend.keySet()){
				methodsList = methodsRecommend.get(clazz);
				System.out.println("Class: "+ clazz.getName());
				if(methodsList!=null)
					System.out.println("\tMethods: "+ methodsList);
			}
		}
	}
	
}
